<?php

ini_set("memory_limit", "100M");
ini_set("max_execution_time", 36000);

require_once('../../config/maptrick.conf.php');
require_once(SITE_ROOT . 'lib/Session.php');
require_once(SITE_ROOT . 'lib/PHT.php');
require_once(SITE_ROOT . 'class/bean/Team.class.php');
require_once(SITE_ROOT . 'class/db/DBTeam.class.php');
require_once(SITE_ROOT . 'class/logic/LogicCoolness.class.php');

try {
	$db = new DBTeam();
	$logic = LogicCoolness::getInstance();
	$count = 0;

	$HT = new CHPPConnection(VERSION, CHPPID, CHPPKEY, SITE_ROOT . 'tmp/hattrick/');
	if ($HT->connectUser(HTUSER, HTSECCODE)) {
		$_SESSION['HT'] = $HT;
		
		$teams = $db->getTeams();
		foreach ($teams as $team) {
			try {
				$logic->autoUpdate($team->getTeamID(), false);
				$count++;
				if ($count == 100) {
					$db = new DBTeam();
					$HT->clearLeagues();
					$HT->clearTeams();
					$_SESSION['HT'] = $HT;
					$count = 0;
					sleep(1);
				}
			}
			catch (IsBotException $ibe) {}
			catch (LeagueNotAvailableException $lnae) {}
			catch (Exception $e) {
				echo "Update failed at team " . $team->getTeamID();
			}
		}
		$HT->disconnect();
		echo "Update was successfull!";
	}
	else echo "Login error!";
}
catch (Exception $e) {
	echo $e->getMessage();
}

?>